<template>
  <page-header-wrapper>
    <page-tab-list></page-tab-list>
    <a-card :border='false'>
      <div v-loading='loading' class='box'>
        <a-alert message='此配置用于平台支付宝收款' type='info' show-icon style='margin: 10px 0 20px 15px' />
        <a-form-model
          ref='wechatForm'
          :rules='wechatRules'
          :model='wechatInfo'
          :labelCol='{ span: 3 }'
          :wrapperCol='{ span: 12 }'
        >
          <a-form-model-item label='支付宝AppId' prop='app_id'>
            <a-input v-model='wechatInfo.app_id' type='text' />
          </a-form-model-item>
          <a-form-model-item label='User ID' prop='user_id'>
            <a-input v-model='wechatInfo.user_id' type='text' />
          </a-form-model-item>
          <a-form-model-item label='支付宝商户名称' prop='name'>
            <a-input v-model='wechatInfo.name' type='text' />
          </a-form-model-item>
          <a-form-model-item label='Aes Key' prop='aes_key'>
            <a-input v-model='wechatInfo.aes_key' type='text' />
          </a-form-model-item>
          <a-form-model-item label='支付宝公钥' prop='alipay_public_key'>
            <a-input v-model='wechatInfo.alipay_public_key' :autoSize='{ minRows: 4, maxRows: 6 }' type='textarea' />
          </a-form-model-item>
          <a-form-model-item label='应用公钥' prop='app_public_key'>
            <a-input v-model='wechatInfo.app_public_key' :autoSize='{ minRows: 4, maxRows: 6 }' type='textarea' />
          </a-form-model-item>
          <a-form-model-item label='应用私钥' prop='app_private_key'>
            <a-input v-model='wechatInfo.app_private_key' :autoSize='{ minRows: 4, maxRows: 6 }' type='textarea' />
          </a-form-model-item>
          <a-form-model-item :wrapper-col='{ span: 24 }'>
            <a-divider style='margin: 0 0 40px 0' />
            <div style='display: flex; justify-content: center'>
              <a-button :loading='btnLoading' type='primary' @click='submitForm' style='margin-right: 8px'> 保存
              </a-button>
              <a-button @click='$router.back()'> 取消</a-button>
            </div>
          </a-form-model-item>
        </a-form-model>
      </div>
    </a-card>

  </page-header-wrapper>
</template>

<script>
import { getPlatformAlipaySetting, setPlatformAlipaySetting } from '@/api/saas/setting'

export default {
  name: 'SaasSettingWechatPay',
  data() {
    return {
      loading: false,
      btnLoading: false,
      wechatInfo: {
        app_id: '',
        user_id: '',
        name: '',
        aes_key: '',
        alipay_public_key: '',
        app_public_key: '',
        app_private_key: ''
      },
      wechatRules: {
        app_id: [{ required: true, message: '请输入支付宝AppId', trigger: 'blur' }],
        user_id: [{ required: true, message: '请输入User ID', trigger: 'blur' }],
        name: [{ required: true, message: '请输入支付宝商户名称', trigger: 'blur' }],
        aes_key: [{ required: true, message: '请输入Aes Key', trigger: 'blur' }],
        alipay_public_key: [{ required: true, message: '请输入支付宝公钥', trigger: 'blur' }],
        app_public_key: [{ required: true, message: '请输入应用公钥', trigger: 'blur' }],
        app_private_key: [{ required: true, message: '请输入应用私钥', trigger: 'blur' }]
      },
      // 微信模板列表
      templateList: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData: function() {
      this.loading = true
      getPlatformAlipaySetting({})
        .then((res) => {
          if (res.code == 0) {
            if (res.data) {
              this.wechatInfo = res.data
            }
          }
        })
        .catch((error) => {
          console.log(error)
        })
        .then(() => {
          this.loading = false
        })
    },
    submitForm: function() {
      this.$refs.wechatForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          setPlatformAlipaySetting(this.wechatInfo)
            .then((res) => {
              if (res.code == 0) {
                this.$message.success('保存成功！')
              } else {
                this.$message.warning(res.msg ? res.msg : '保存失败！')
              }
            })
            .catch((error) => {
              console.log(error)
            })
            .then(() => {
              this.btnLoading = false
            })
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
